<?php


namespace App\Http\Controllers;


use App\Models\Apply;
use App\Models\Visitor;
use Illuminate\Http\Request;

class ApplyController extends Controller
{
    public function index(Request $request)
    {
        return redirect('/admin');
    }
    public function store(Request $request)
    {

        $this->validate($request,[
            'name'=>'required',
            'phone'=>'required',
            'idcard'=>'required',
        ]);
        $this->repeatedSubmitCheck();

        try {
            Apply::query()->create($request->all());
            return $this->success();
        }
        catch (\Exception $exception)
        {
            return  $this->fail($exception->getMessage());
        }
    }

    public function statistics(Request $request)
    {
        #当前url
        $url=$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        #获取ip和来源
        $address = GetIpFrom();
        $froms = $address[0];
        $ip = $address[1];
        #获取浏览器和系统类型
        $broswer = get_broswer();
        $os = get_os();

        #获取最后来源地址
        if(empty($_SERVER['HTTP_REFERER'])){
            $source_link = $url;
        }else{
            $source_link = $_SERVER['HTTP_REFERER'];
        }

        Visitor::query()->create([
            'ip'=>$ip,
            'froms'=>$froms,
            'system'=>$os,
            'browser'=>$broswer,
            'pageview'=>$url,
            'source_link'=>$source_link,
        ]);
        return $this->success();
    }

}
